package dyyx.util;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import javax.management.MBeanServerConnection;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;

import org.apache.commons.lang3.StringUtils;
import org.apache.zookeeper.ZooKeeper;

import dyyx.dto.JmxClient;
import dyyx.dto.ZkClient;

public class ClientUtil {
	
	private static final Map<String,ZkClient> zkclients = new ConcurrentHashMap<String,ZkClient>();
	private static final Map<String,JmxClient> jmxclients = new ConcurrentHashMap<String,JmxClient>();

	
	public static synchronized void resetZkClient(String name)throws Exception{
		if(StringUtils.isBlank(name)){
			throw new Exception("name is blank");
    	}
    	ZkClient zkClient = zkclients.get(name);
    	if(zkClient==null){
    		throw new Exception("zkClient not exist");
    	}
    	
    	zkClient.zk.close();
    	zkclients.remove(name);
    	
	}
	
    public static synchronized ZkClient getZkClient(String name)throws Exception{
    	if(StringUtils.isBlank(name)){
    		throw new Exception("name is blank");
    	}
    	ZkClient zkClient = zkclients.get(name);
    	if(zkClient!=null){
    		return zkClient;
    	}
    	String connectString = ConfigUtil.get(name, "connectString");
    	if(StringUtils.isBlank(connectString)){
    		throw new Exception("connectString is blank");
    	}
    	
    	String sessionTimeoutStr = ConfigUtil.get(name, "sessionTimeout");
    	int sessionTimeout = CommUtil.getInt(sessionTimeoutStr);
    	if(sessionTimeout<=0){
    		sessionTimeout = 3000;
    	}
    	
    	ZooKeeper zk = new ZooKeeper(connectString,sessionTimeout,null);
    	
    	zkClient = new ZkClient(zk,name,connectString,sessionTimeout);
    	
    	zkclients.put(name, zkClient);
    	
    	
    	return zkClient;
    }
    
    
    /////
    
	public static synchronized void resetJmxClient(String name)throws Exception{
		if(StringUtils.isBlank(name)){
			throw new Exception("name is blank");
    	}
    	JmxClient jmxClient = jmxclients.get(name);
    	if(jmxClient==null){
    		throw new Exception("jmxClient not exist");
    	}
    	
    	jmxClient.close();
    	jmxclients.remove(name);
	}

    public static synchronized JmxClient getJmxClient(String name)throws Exception{
    	if(StringUtils.isBlank(name)){
    		throw new Exception("name is blank");
    	}
    	JmxClient jmxClient = jmxclients.get(name);
    	if(jmxClient!=null){
    		return jmxClient;
    	}
    	String connectString = ConfigUtil.get(name, "connectString");
    	if(StringUtils.isBlank(connectString)){
    		throw new Exception("connectString is blank");
    	}
    	
    	JMXServiceURL serviceURL = new JMXServiceURL(connectString);  
		JMXConnector jmxConnector = JMXConnectorFactory.connect(serviceURL);  
		MBeanServerConnection mbeanServerConnection = jmxConnector.getMBeanServerConnection();  
		jmxClient = new JmxClient(jmxConnector,mbeanServerConnection,name,connectString);
    	
    	jmxclients.put(name, jmxClient);

    	return jmxClient;
    }

    

   

}
